package com.drotposta.unitils;

import java.lang.reflect.Field;
import javax.persistence.PersistenceContext;

/**
 *
 * @author elek
 */
public class EntityManagerInjector extends BaseInjector {

    private Directory directory;

    public EntityManagerInjector(Directory directory) {
        this.directory = directory;
    }

    @Override
    public Object getValueToInject(Object bean, Field field, Class annotation) {
        PersistenceContext context = (PersistenceContext) field.getAnnotation(annotation);
        String unitName = context.unitName();
        if (unitName == null) {
            return directory.getEntityManager();
        } else {
            return directory.getEntityManager(unitName);
        }
    }
}
